import logging
import os
from datetime import timedelta
from typing import List


class BaseConfig:
    # jwt
    # jwt

    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or 'lsz666'
    JWT_COOKIE_CSRF_PROTECT = True
    JWT_CSRF_CHECK_FORM = True

    JWT_ACCESS_TOKEN_EXPIRES =3600
    PROPAGATE_EXCEPTIONS = True


    SYSTEM_NAME = os.getenv('SYSTEM_NAME', '小咖啡')
    # 主题面板的链接列表配置
    SYSTEM_PANEL_LINKS = [
        {
            "icon": "layui-icon layui-icon-auz",
            "title": "官方网站",
            "href": "http://www.pearadmin.com"
        },
        {
            "icon": "layui-icon layui-icon-auz",
            "title": "开发文档",
            "href": "http://www.pearadmin.com"
        },
        {
            "icon": "layui-icon layui-icon-auz",
            "title": "开源地址",
            "href": "https://gitee.com/Jmysy/Pear-Admin-Layui"
        }
    ]
    # 用户头像
    UPLOADED_PHOTOS_DEST = 'static/upload/useravatar'
    # 商品
    UPLOADED_PHOTOS_GOODS = 'static/upload/goods'

    # UPLOADED_FILES_ALLOW: list[str] = ['gif', 'jpg']

    # JSON配置
    JSON_AS_ASCII = False

    SECRET_KEY = os.getenv('SECRET_KEY', 'lsz666') or 'lsz666'

    # redis配置

    # REDIS_HOST = os.getenv('REDIS_HOST') or '127.0.0.1'
    CACHE_REDIS_PASSWORD = 'lsz666'
    REDIS_PORT = int(os.getenv('REDIS_PORT') or 6379)
    PERMANENT_SESSION_LIFETIME = timedelta(hours=3)
    # redis 缓存
    redis_config = {
        'CACHE_TYPE': 'redis',
        'CACHE_REDIS_HOST': '124.221.213.179',
        'CACHE_REDIS_PORT': 6379
    }

    # mysql 配置


    # 默认日志等级
    LOG_LEVEL = logging.WARN
    #
    MAIL_SERVER = os.getenv('MAIL_SERVER') or 'smtp.qq.com'
    MAIL_USE_TLS = False
    MAIL_USE_SSL = True
    MAIL_PORT = 465
    MAIL_USERNAME = os.getenv('MAIL_USERNAME') or '123@qq.com'
    MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') or 'XXXXX'  # 生成的授权码
    # 默认发件人的邮箱,这里填写和MAIL_USERNAME一致即可
    MAIL_DEFAULT_SENDER = ('pear admin', os.getenv('MAIL_USERNAME') or '123@qq.com')


class TestingConfig(BaseConfig):
    """ 测试配置 """
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'  # 内存数据库


class DevelopmentConfig(BaseConfig):
    """ 开发配置 """
    # mysql
    MYSQL_USERNAME = os.getenv('MYSQL_USERNAME') or "root"
    MYSQL_HOST = os.getenv('MYSQL_HOST') or "127.0.0.1"
    MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD') or "lsz990324"
    MYSQL_PORT = int(os.getenv('MYSQL_PORT') or 3306)
    MYSQL_DATABASE = os.getenv('MYSQL_DATABASE') or "bs_new"
    # mysql 数据库的配置信息
    SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"

    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SQLALCHEMY_ECHO = False


class ProductionConfig(BaseConfig):
    """生成环境配置"""
    # mysql
    MYSQL_USERNAME = os.getenv('MYSQL_USERNAME') or "root"
    MYSQL_HOST = os.getenv('MYSQL_HOST') or "124.221.213.179"
    MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD') or "pkaBaskxnPSnJSi3"
    MYSQL_PORT = int(os.getenv('MYSQL_PORT') or 3309)
    MYSQL_DATABASE = os.getenv('MYSQL_DATABASE') or "flask_new"
    # mysql 数据库的配置信息
    SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"
    # redis
    REDIS_HOST = os.getenv('REDIS_HOST') or "124.221.213.179"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = False
    SQLALCHEMY_POOL_RECYCLE = 8

    LOG_LEVEL = logging.ERROR


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig
}

if __name__ == '__main__':
    dev = DevelopmentConfig()
    print(dev.SYSTEM_NAME)
    print(dev.MYSQL_USERNAME)
